package com.zfl;

import java.util.Arrays;

public class ProductExceptSelf {

    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3};
        new ProductExceptSelf().productExceptSelf(nums);
    }

    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] prefixProduct = new int[n + 1];
        int[] suffixProduct = new int[n + 1];

        prefixProduct[0] = 1;
        suffixProduct[n] = 1;

        for (int i = 1; i < n + 1; i++) {
            prefixProduct[i] = prefixProduct[i - 1] * nums[i - 1];
        }
        for (int i = n - 1; i >= 0; i--) {
            suffixProduct[i] = suffixProduct[i + 1] * nums[i];
        }
        System.out.println("suffixProduct = " + Arrays.toString(suffixProduct));

        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = prefixProduct[i]*suffixProduct[i+1];
        }
        return res;
    }
}
